﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using System.ComponentModel;

namespace SSCCMembership
{
    public partial class BusyIndicator : UserControl
    {
        public BusyIndicator()
        {
            InitializeComponent();
            if (DesignerProperties.IsInDesignTool)
                return;
            BI = this;
            ConnectivityManager.CheckNetworkStatus();
        }
        static BusyIndicator()
        {
            DT.Tick += new EventHandler(DT_Tick);
        }

        static void DT_Tick(object sender, EventArgs e)
        {
            DT.Stop();
            Hide();
            CloseAction();
        }   
        static BusyIndicator BI;
        static DispatcherTimer DT = new DispatcherTimer();
        static bool EnableUserClose = false;
        static Action CloseAction;

        public static void Show(string Message = "Loading", bool HasProgress = true, double SecondsTimeout = 0, bool EnableUserClose = false)
        {
            BusyIndicator.CloseAction = ()=>{};
            if (!BI.Dispatcher.CheckAccess())
            {
                BI.Dispatcher.BeginInvoke(() => Show(Message, HasProgress, SecondsTimeout));
                return;
            }
            if (HasProgress)
            {
                BI.ProgressBar.IsIndeterminate = true;
                BI.R2.MaxHeight = 1000;
            }
            else
            {
                BI.ProgressBar.IsIndeterminate = false;
                BI.R2.MaxHeight = 0;
            }
            BI.Message.Text = Message;
            if (SecondsTimeout != 0)
            {
                DT.Interval = TimeSpan.FromSeconds(SecondsTimeout);
                DT.Start();
            }
            BusyIndicator.EnableUserClose = EnableUserClose;
            BI.LayoutRoot.Visibility = Visibility.Visible;
            BI.MessageDisplayBTN.Focus();
        }
        public static void Show(Action CloseAction, string Message = "Loading", bool HasProgress = true, double SecondsTimeout = 0, bool EnableUserClose = false)
        {
            Show(Message, HasProgress, SecondsTimeout, EnableUserClose);
            BusyIndicator.CloseAction = CloseAction;
        }
        
        public static void Hide()
        {
            if (BI.Dispatcher.CheckAccess())
            {
                BI.ProgressBar.IsIndeterminate = false;
                BI.LayoutRoot.Visibility = Visibility.Collapsed;
            }
            else
            {
                BI.Dispatcher.BeginInvoke(() => Hide());
            }
        }

        //private void MessageDisplay_KeyUp(object sender, KeyEventArgs e)
        //{
        //    if (EnableUserClose)
        //    {
        //        Hide();
        //    }
        //}

        private void MessageDisplay_Click(object sender, RoutedEventArgs e)
        {
            if (EnableUserClose)
            {
                Hide();
            }
        }
    }
}
